import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_app/r.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_app/widget/empty_view.dart';
import 'package:flutter_app/widget/gallery/gallery.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'photo_select_logic.dart';
import 'photo_select_state.dart';

/**
 * @Description: 相册 可视化采集资料 选择
 * @Author: 下雨了
 * @CreateDate: 2022-7-15 15:38:12
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-7-15 15:38:12
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
class PhotoSelectPage extends StatefulWidget {
  final String title;
  const PhotoSelectPage({Key? key, required this.title}) : super(key: key);

  @override
  _PhotoSelectPageState createState() => _PhotoSelectPageState();
}

class _PhotoSelectPageState extends State<PhotoSelectPage> {
  final logic = Get.put(PhotoSelectLogic());
  final state = Get.find<PhotoSelectLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PhotoSelectLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: MyColors.FFF1F1F1,
        appBar: MyAppBar(context, title: "选择图片/视频证据"),
        body: Container(
          margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 11.r, bottom: 11.r),
          child: (state.entity?.length ?? 0) > 0
            ? ListView.separated(
              padding: EdgeInsets.only(top: 0.r),
              itemCount: state.entity?.length ?? 0,
              itemBuilder: (BuildContext context, int index) {
                return ListItem(state.entity![index]);
              },
              separatorBuilder: (BuildContext context, int index) {
                return Divider(color: Colors.transparent, height: 14.5.r,);
              },
            ) : EmptyView(),
        ),
        bottomNavigationBar: Container(
          decoration: BoxDecoration(
              boxShadow: [BoxShadow(
                color: Color(0x1a000000),
                blurRadius: 6.67.r,
                spreadRadius: 0.05.r,
              )],
              shape: BoxShape.rectangle,
              color: Colors.white
          ),
          padding: EdgeInsets.fromLTRB(16.r, 5.r, 16.r, 5.r),
          child: SafeArea(
            child: SizedBox(
              height: 40.r,
              child: TextButton(
                style: TextButton.styleFrom(
                  backgroundColor: MyColors.FF2763B9,
                  padding: EdgeInsets.all(0),
                ),
                child: Text("确认", style: TextStyle(color: MyColors.FFFFFFFF, fontSize: 14.sp),),
                onPressed: () {
                  logic.save();
                },
              ),
            ),
          ),
        ),
      );
    });
  }

  Widget ListItem(PhotoGalleryInfo item) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Visibility(
            visible: item.list?.length != 0,
            child: Text(item.title ?? "", style: TextStyle(color: MyColors.FF666666, fontSize: 14.sp),),
          ),
          SizedBox(height: 10.r,),
          GridView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: item.list?.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                mainAxisSpacing: 2.r,
                crossAxisSpacing: 2.r,
                childAspectRatio: 1),
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                child: Stack(
                  alignment: Alignment.topRight,
                  children: [
                    Container(
                      width: 113.r,
                      height: 113.r,
                      child: Image.file(
                        File(item.list![index].thumbnail ?? ""),
                        fit: BoxFit.cover,
                        width: 113.r,
                      ),
                    ),
                    Visibility(
                      visible: item.list![index].type == "video",
                      child: Center(
                        child: Image.asset(R.assetsImgIcon57, width: 30.r,),
                      ),
                    ),
                    Visibility(
                      visible: item.list![index].type == "video",
                      child: Align(
                        alignment: Alignment(0.9, 0.95),
                        child: Text(item.list![index].duration ?? "", style: TextStyle(color: MyColors.FFFFFFFF, fontSize: 12.sp), ),
                      ),
                    ),
                    Obx(() {
                      return InkWell(
                        child: Container(
                          padding: EdgeInsets.only(top: 5.r, right: 5.r, left: 10.r, bottom: 10.r),
                          child: item.list![index].isChose.value
                            ? Image.asset(R.assetsImgIcon81, width: 18.r,)
                            : Image.asset(R.assetsImgIcon80, width: 18.r,),
                        ),
                        onTap: () {
                          item.list![index].isChose.value = !item.list![index].isChose.value;
                        },
                      );
                    }),
                  ],
                ),
                onTap: () {
                  List<GalleryModel> list = [];
                  item.list?.forEach((element) {
                    list.add(GalleryModel(
                      id: element.path ?? "",
                      url: element.path ?? "",
                      time: element.dateTime,
                      address: element.address,
                    ));
                  });
                  gallery(list, showIndex: index);
                },
              );
            },
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<PhotoSelectLogic>();
    super.dispose();
  }
}
